package be.castanea.client.input;

import be.castanea.client.CastaneaClient;
import be.castanea.client.model.Movement;
import be.castanea.client.model.Self;
import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Castanea
 * 2009
 * @author Geert van Leemputten, Steven Rymenans, Bart Van Hooydonck
 * Ingame input handler
 * Change binds and dispatch events according to received input
 */
public class BindingsHandler extends InputHandler {

    private Self self;
    private Properties properties;

    public BindingsHandler(Self self) {
        this.self = self;
        this.properties = new Properties();
        //try {
        //    properties.load(new FileInputStream("binds.properties"));
        //} catch (IOException ex) {
        // default
        add(Command.FORWARD.toString(), KeyInput.KEY_Z);
        add(Command.BACKWARD.toString(), KeyInput.KEY_S);
        add(Command.STRAFE_LEFT.toString(), KeyInput.KEY_Q);
        add(Command.TURN_LEFT.toString(), KeyInput.KEY_A);
        add(Command.STRAFE_RIGHT.toString(), KeyInput.KEY_D);
        add(Command.TURN_RIGHT.toString(), KeyInput.KEY_E);
        add(Command.EXIT.toString(), KeyInput.KEY_ESCAPE);
    //}
        /*KeyBindingManager.getKeyBindingManager().removeAll();
    Enumeration keyMappings = properties.propertyNames();
    while (keyMappings.hasMoreElements()) {
    String action = (String) keyMappings.nextElement();
    int keyCode = new Integer(properties.getProperty(action)).intValue();
    KeyBindingManager.getKeyBindingManager().add(action, keyCode);
    }*/
    }

    @Override
    public void update(float interpolation) {
        super.update(interpolation);
        if (isValidCommand(Command.EXIT.toString())) {
            Logger.getLogger(BindingsHandler.class.getName()).log(Level.INFO, "exiting...");
            CastaneaClient.disconnect();
            System.exit(0);
        }

        // movement commands
        Movement currentMovement = new Movement();
        if (isValidCommand(Command.FORWARD.toString())) {
            currentMovement.addForward(Self.getInstance().getMaxSpeed());
        }
        if (isValidCommand(Command.BACKWARD.toString())) {
            currentMovement.addForward(-1 * Self.getInstance().getMaxSpeed());
        }
        if (isValidCommand(Command.STRAFE_LEFT.toString())) {
            currentMovement.addSideway(Self.getInstance().getMaxSpeed());
        }
        if (isValidCommand(Command.TURN_LEFT.toString())) {
            currentMovement.addTurnSpeed(4);
        }
        if (isValidCommand(Command.STRAFE_RIGHT.toString())) {
            currentMovement.addSideway(-1 * Self.getInstance().getMaxSpeed());
        }
        if (isValidCommand(Command.TURN_RIGHT.toString())) {
            currentMovement.addTurnSpeed(-4);
        }
        if (!currentMovement.equals(Self.getInstance().getMovement())) {
            Self.getInstance().setMovement(currentMovement);
        }
    // end movement commands
    }

    private void add(String command, int keyCode) {
        //try {
        KeyBindingManager.getKeyBindingManager().add(command, keyCode);
        properties.put(command, keyCode);
    //properties.store(new FileOutputStream("binds.properties"), "Castanea Key Binds");
    //} catch (IOException ex) {
    //    Logger.getLogger(BindingsManager.class.getName()).log(Level.SEVERE, null, ex);
    //}
    }

    private boolean isValidCommand(String command) {
        return KeyBindingManager.getKeyBindingManager().isValidCommand(command);
    }
}
